<?php

class Test_PicStore {

    private $_instance = null;

    public function __construct() {

        V::addLibrary('third', dirname(V_PATH) . '/common-lib/third');
        $conf = array(
            'aws_client' => array(
                'profile' => 'default',
                'credentials_filename' => dirname(__FILE__) . '/PicStore/aws.credentials',
                'region' => 'ap-southeast-1',
                'version' => 'latest'
            ),
            'picstore' => array(
                'storage' => array(
                    'engine' => 'AwsS3',
                    'option' => array(
                        'base_url' => 'https://s3-ap-southeast-1.amazonaws.com',
                        'bucket' => 'hsbs1',
                    ),
                ),
                'cate' => array(
                    // 手机外观图片
                    'phonephoto' => array(
                        'type_limit' => array(Image::TYPE_JPG),
                        'size_limit' => 5000000,
                        'watermark' => array(
                        ),
                        'thumb' => array(),
                    ),
                    // 杂项，默认
                    'misc' => array(
                        'type_limit' => array(Image::TYPE_GIF, Image::TYPE_JPG, Image::TYPE_PNG),
                        'size_limit' => 2000000,
                        'watermark' => array(
                        ),
                        'thumb' => array(),
                    ),
                ),
            )
        );
        V::loadConfig($conf);
        $this->_instance = PicStore::getInstance();
    }

    public function testStore() {

        $url = 'https://s3-ap-southeast-1.amazonaws.com/hsbproductpic/a8ff1ebe128fb9d2a30bc120c1c7cea9.jpg';
        $content = file_get_contents($url);
        $tmpDir = FS::tmpDir();
        $imagePath = $tmpDir . '/test-pic.jpg';
        file_put_contents($imagePath, $content);

        $resourceId = $this->_instance->store($imagePath, 'phonephoto');
        echo 'id: ', $resourceId, PHP_EOL;
        echo 'url: ', $this->_instance->getUrl($resourceId), PHP_EOL;

        FS::rmdir($tmpDir);
        return true;
    }

}
